
// self.importScripts('./spark-md5.min.js');
import SparkMD5 from "spark-md5";

self.onmessage = async (e) => {
  console.log("🚀 ~ web worker ~ e:", e)
  const file = e.data.file;
  const chunkSize = 1 * 1024 * 1024; // 分片计算，避免卡死
  const chunks = Math.ceil(file.size / chunkSize);
  const spark = new SparkMD5.ArrayBuffer();
  let currentChunk = 0;
  
  const loadNext = () => {
    const reader = new FileReader();
    const start = currentChunk * chunkSize;
    const end = start + chunkSize >= file.size ? file.size : start + chunkSize;
    
    reader.readAsArrayBuffer(file.slice(start, end));
    reader.onload = e => {
      spark.append(e.target.result);
      currentChunk++;
      
      if (currentChunk < chunks) {
        loadNext();
      } else {
        self.postMessage(spark.end());
      }
    }
  }
  
  loadNext()
}
